<?php
/**
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2017/5/8
 * Time: 19:16
 */

namespace SDXApi\Controller;


use Think\Controller;
use Common\Api\Ali_ossApi;
use Think\Log;
use Think\Model\MergeModel;


/*
第三方订单状态标识
0 默认未处理订单
1 配送中
2 已撤销  待回仓
3 修改配送时间 待回仓
4 已撤销 已回仓
5 修改配送时间 已回仓
6 已送达
*/
class GkshippingController extends Controller{

    private $key = '';
    private $source = '';

    public function __construct(){
        $this->key = C('gk_key');
        $this->source = C('gk_source');
    }

    /**
     * 验证签名
     * @param $order_sn
     * @param $sign
     * @return bool
     */
    public function verifyNotify($order_sn,$sign){
        $verify_sign = md5($this->key.$order_sn);

        if($verify_sign==$sign){
            return true;
        }else{
            return false;
        }
    }

    public function write_log($message,$filename){
        $log = new Log();
        $destination = C('LOG_PATH').date('y_m_d').'_'.$filename.'.log';

        $log->write($message,'DEBUG','',$destination);
    }

    public function Gknotify()
    {
        $order_sn = I('post.order_no');
        $sign = I('post.sign');
        $status = I('post.status');
        $time = I('post.time');
        $msg = I('post.msg');
        $_POST['store'] = 1;
        $is_not_send = I('post.is_not_send');

        $shipping_extend_type = "gk";
        $exman_name =  I('post.exman_name');        //快递员姓名
        $exman_mobile = I('post.exman_mobile');     //快递员手机

        if (empty($order_sn) || (empty($sign) && $status!='90')) {
            returnApiError('参数错误');
        }

        if (!$is_not_send && (!$this->verifyNotify($order_sn, $sign) && $status!='90')) {
            returnApiError('签名错误');
        }

        if(!empty($exman_name) && !empty($exman_mobile)){
            $extend_data = array(
                'shipping_extend_type'=>$shipping_extend_type,
                'shipping_exman_name'=>$exman_name,
                'shipping_exman_mobile'=>$exman_mobile
            );
        }

        $this->write_log('gk test order_no:' . $order_sn . ' ,status:' . $status . ' ,time: ' . $time . ' ,msg:' . $msg.',data:'.json_encode($_POST), 'gk_shipping');

        //如果是同一个重新推送的订单，获取正确的订单编号
        if(!empty($shipping_extend_type) && strpos($order_sn, $shipping_extend_type) !== false){
            $temp_order_sn = explode($shipping_extend_type,$order_sn);
            $order_sn = $temp_order_sn[0];
        }

        $order_info = M('order_info')->where(array('order_sn'=>$order_sn))->find();

        $user_id_list = C('gk_peisong_user_id');
        $user_id = $user_id_list[$order_info['wid']];

        $res = false;
        switch ($status) {
            case '10' :
                $verify_code = I('post.verify_code');
                $this->distribute($user_id, $order_sn,$status,$verify_code);
            break;
            case '5' :
                $this->distributed($user_id, $order_sn,$status);
            break;
            case '9' :
                $action = "外配商品需回仓，等待仓库进行回仓操作";
                if(!empty($msg)){
                    $action.="(".$msg.")";
                }
                $this->order_back($order_sn,$user_id,$action,$status);
//                $res = $this->add_order_action($order_sn, $user_id, $action);
//                if ($res) {
//                    //加入预警记录
//                    $this->add_alarm_shipping_log($order_sn, $status, $msg);
//                }
            break;
            case '4' :
                $action = "已有配送员接单";
                $res = $this->add_order_action($order_sn, $user_id, $action,$extend_data);
            break;
            case '89' :
                $action = "配送员已进行配送订单货品";
                $res = $this->add_order_action($order_sn, $user_id, $action);
            break;
            case '90' :
                $deliver_time = I('post.deliver_time');
                $deliver_time_end = I('post.deliver_time_end');

                $action = "极客订单的异常信息（" . $msg . "）";
                if(!empty($deliver_time) && !empty($deliver_time_end)){
                    $param = array('best_time_start'=>strtotime($deliver_time),'best_time'=>strtotime($deliver_time_end));
                    $this->change_distribution_time($order_sn,$user_id,$action,$status,$param);
                }else{
//                    $this->order_back($order_sn,$user_id,$action,$status);
                    $res = $this->add_order_action($order_sn, $user_id, $action);
                }


//                $res = $this->add_order_action($order_sn, $user_id, $action);
//                if ($res) {
//                    //加入预警记录
//                    $this->add_alarm_shipping_log($order_sn, $status, $msg);
//                }
            break;
            case '16' :
                $action = "外派人员(极客)未能接收订单货品(".$msg.")";
                $res = $this->add_order_action($order_sn, $user_id, $action);
                if ($res) {
                    //加入预警记录
                    $this->add_alarm_shipping_log($order_sn, $status, $msg);
                }
            break;
            case '3' :
                $action = "订单取消配送（" . $msg . "）";
                $this->order_back($order_sn,$user_id,$action,$status);
            break;
        }
        if ($res) {
            $this->return_gk_success('订单状态修改成功');
        } else {
            $this->return_gk_error('订单状态修改失败');
        }
    }

    public function change_distribution_time($order_sn,$user_id,$action,$status,$param){
        if(empty($order_sn)){
            $this->return_gk_error('订单号为空');
        }else{
            $deploy_info = A("Distribution")->get_admin_user_name($user_id);

            $order_info = M('order_info')->where(array('order_sn'=>$order_sn))->find();
            if(empty($order_info)){
                $this->return_gk_error('订单不存在');
            }

            $data = $param;

            $dtime=time();
            $dftime=date('Y-m-d',$dtime);
            $order_best_time=date('Y-m-d',$data['best_time']);
            $action_note = $action."//已延迟配送";

            if($order_best_time!=$dftime) {
                $data['shipping_status'] = 5;
                $data['station'] = 1;
                $data['station_id'] = 0;
                $data['station_time'] = 0;
                $data['wp_userid'] = 0;
            }

            $diff_time = round(($data['best_time_start']-$order_info['best_time_start'])/3600/24);

            //修改期望送达时间
            $action_note .= " 由 [".date('Y-m-d H:i:s',$order_info['best_time_start'])."] - [".date('Y-m-d H:i:s',$order_info['best_time'])."]";

            $action_note .= " 延迟至 [".date('Y-m-d H:i:s',$data['best_time_start'])."] - [".date('Y-m-d H:i:s',$data['best_time'])."]";

            $change_order =  false;

            if($diff_time>1){
                //配送员延迟两天以上，改预售单
                $data['shipping_status'] = 0;
                $data['wp_userid'] = 0;
//                $data['sorting_id'] = 0;
//                $data['sorting_time'] = 0;
                $change_order =  true;
                $data['outgo'] = 2;
                $data['station'] = 3;
                $data['station_id'] = 0;
                $data['station_time'] = 0;
                $data['shipping_extend_status'] = $this->get_shipping_extend_status($status);    //第三方订单操作状态
                $huic_test = "<span style='color: #ff0000;'>订单需要回仓，请将商品拿回总仓</span>";
            }

            if($change_order){
                //处理订单现预售转变
                A("Distribution")->change_order_pre_sale($order_info['order_id'],$deploy_info);
            }
            $map = array('order_id'=>$order_info['order_id']);
            $re = M('order_info')->where($map)->save($data);

            if(empty($data['shipping_status'])){
                $data['shipping_status'] = M('order_info')->where($map)->getField("shipping_status");
            }
            $a = array (
                'order_id' => $order_info['order_id'],
                'user_id' => $user_id,
                'order_status' => OS_CONFIRMED,
                'shipping_status' => $data['shipping_status'],
                'pay_status' => PS_PAYED,
                'action_note' => $action_note."</br>".$huic_test
            );
            A("Distribution")->record_diliveryman_log ( $a ); // 记录配送员操作日志

            $datetime = date("Y-m-d",$data['best_time_start']);
            $smsremark['best_time'] = $datetime.' '.date("H:i:s",$data['best_time_start']).'-'.date("H:i:s",$data['best_time']);
            $smsremark['action'] = "配送时间已更改";

            //发送短信通知
            //$send_result = A("Smsapi")->send_message_api('delayed_delivery_notice',$order['mobile'],$order['consignee'],0,$order['order_sn'],0,$order['user_id'],$smsremark);
            $send_result = A("Smsapi")->send_message_api('delayed_order',$order_info['mobile'],$order_info['consignee'],0,$order_info['order_sn'],0,$order_info['user_id'],$smsremark);

            if($re){
                $this->return_gk_success(null,'修改配送时间成功');
            }else{
                $this->return_gk_error('修改配送时间失败');
            }

        }
    }

    /**
     * 开始配送
     * @param $user_id
     * @param $order_sn
     * @param $status
     * @param $verify_code
     */
    public function distribute($user_id,$order_sn,$status,$verify_code){
        if(!isset($user_id) || !isset($order_sn)){
            $this->return_gk_error('参数错误');
        }
        $wp=A("Distribution")->check_warehouse_personnel($user_id);
        if(!$wp){
            $this->return_gk_error('非法用户或者该用户已被禁用');
        }

        $where['order_sn']=$order_sn;
        $where['pay_status']=array("in","2,3");
        $where['order_status']=array("neq","2");
        $list=M('order_info')->field('order_id,order_sn,user_id,order_status,shipping_status,pay_status,mobile,consignee,best_time,best_time_start,pay_time,is_presell,wid,shipping_extend_type,shipping_exman_name,shipping_exman_mobile')->where($where)->find();
        if($list['order_id']){
            if($list['order_status']==5 && $list['shipping_status']==2){
                $this->add_order_action($order_sn, $user_id, '已发货','',$list);
                $this->return_gk_success('此单已确认收货，不用重复配送');
            }
            try {
                $data['order_status']=5;
                $data['shipping_status']=1;
                $data['shipping_time']=gmtime();
                $data['wp_userid'] = $user_id;
                $data['shipping_extend_status'] = $this->get_shipping_extend_status($status);    //第三方订单操作状态
                $id=M('order_info')->where($where)->save($data);
                if($id!=false){
                    $a=array(
                        'order_id'=>$list['order_id'],
                        'user_id'=>$user_id,
                        'order_status'=>5,
                        'shipping_status'=>1,
                        'pay_status'=>$list['pay_status'],
                        'action_note'=>'已发货'
                    );

                    if(!empty($list['shipping_exman_name']) && !empty($list['shipping_exman_mobile'])){
                        $extend_data = array(
                            'shipping_extend_type'=>$list['shipping_extend_type'],
                            'shipping_exman_name'=>$list['shipping_exman_name'],
                            'shipping_exman_mobile'=>$list['shipping_exman_mobile']
                        );
                    }


                    A("Distribution")->record_diliveryman_log($a,$extend_data);//记录配送员操作日志
                    A("Distribution")->record_logistics($list['order_id'],1,"订单已由:".$extend_data['shipping_exman_name']." [ ".$extend_data['shipping_exman_mobile']." ] 开始配送");
                    $is_not_send = I('post.is_not_send');
                    if(!$is_not_send){
                        //获取收货验证码
                        $sms_data['order_id'] = $list['order_id'];
                        $sms_data['code'] = $verify_code;
                        M("order_verif_code")->add($sms_data,$options=array(),$replace=true);
                        $remark['code'] = $verify_code;

                        $remark['phone'] = $extend_data['shipping_exman_mobile'];

                        //初始化时区，解决时区不正确的问题
                        date_default_timezone_set('PRC');
                        if($list['best_time_start']==0){
                            if($list['is_presell']==1){
                                $new_best_time = date("Y-m-d",$list['best_time'])." 20:30:00";
                                $list['best_time'] = strtotime($new_best_time)-1;
                                $list['best_time_start'] = strtotime(date("Y-m-d",$list['best_time'])." 10:00:00");
                            }else{
                                $list['best_time_start'] = $list['best_time']-2*3600;
                            }
                        }
                        $datetime = date("Y-m-d",$list['best_time_start']);

                        $remark['best_time'] = $datetime.' '.date("H:i:s",$list['best_time_start']).'-'.date("H:i:s",$list['best_time']);

                        $wechat_param=array();

                        switch($_SERVER['SERVER_NAME']){
                            case 'api.xian3.com':
                                $thisurl='xian3.com';
                            break;
                            case 'api.test.shidexian.com':
                                $thisurl='test.shidexian.com';
                            break;
                            case 'api.shidexian.com':
                                $thisurl='w.shidexian.com';
                            break;
                            default:
                                $thisurl='w.shidexian.com';
                            break;
                        }
                        $oid=$list['user_id'];
                        $Code=md5($oid.'sdx@9999');

                        $goods_name=M("order_goods")->where(array('order_id'=>$list['order_id'],'goods_id'=>array('EXP','not in(select goods_id from sdx_refund_detail where order_id='.$list['order_id'].')')))->limit(1)->getField("goods_name");
                        $goods_number=M("order_goods")->where(array('order_id'=>$list['order_id'],'goods_id'=>array('EXP','not in(select goods_id from sdx_refund_detail where order_id='.$list['order_id'].')')))->count();

                        //                    $louxiaoer_info=A("Smsapi")->get_louxiaoer_info($list['order_id']);
                        //
                        //                    $housekeeper='';
                        //                    if(!empty($louxiaoer_info['name']) && !empty($louxiaoer_info['phone'])){
                        //                        $housekeeper="您的专属客服楼管家为".$louxiaoer_info['name']."-".$louxiaoer_info['phone']."，任何订单问题均可微信联系楼管家，还能最高返现8元。祝生活愉快！";
                        //                    }else{
                        //                        $housekeeper="联系楼管家还能再返现。如需添加，请于微信公众号留言。";
                        //                    }

                        $name = $extend_data['shipping_exman_name'];

                        $wechat_param['url']="https://".$thisurl."/?index&oid=".$oid."&code=".$Code."&lll=#!/ordInfo?ordId=".$list['order_id'];
                        $wechat_param['first']='尊敬的'.$list['consignee'].'，您所订购的'.$goods_name.'等'.$goods_number.'件商品已经在配送中，预计到达时间为'.$remark['best_time'].'，请保持手机畅通。';
                        $wechat_param['keyword1']=$list['order_sn'];
                        $wechat_param['keyword2']=$name;
                        $wechat_param['keyword3']=$remark['phone'];
                        $wechat_param['remark']=array("您的订单收货码为：[ ".$verify_code." ],确认收货时请将此码提供给配送员。如代收，请把该码提供给代收人","#FF0000");

                        A("Smsapi")->send_message_api('receipt_verification',$list['mobile'],$list['consignee'],0,$list['order_sn'],0,$list['user_id'],$remark,'',$wechat_param);
                    }
                    $this->return_gk_success(array('id'=>$id),'配送订单成功');
                    /*}else{
                     returnApiError('配送订单失败');
                    }*/
                }else{
                    $this->return_gk_error('配送订单失败');
                }
            }catch(Exception $e){
                $this->return_gk_error($e->getMessage());
            }
        }else{
            $this->return_gk_error('查无此订单/订单状态不正常');
        }

    }

    /**
     * 配送结束
     * @param $user_id
     * @param $order_sn
     * @param $status
     */
    public function distributed($user_id,$order_sn,$status){
        if(!isset($user_id) || !isset($order_sn)){
            $this->return_gk_error('参数错误');
        }
        $wp=A("Distribution")->check_warehouse_personnel($user_id);
        if(!$wp){
            $this->return_gk_error('非法用户或者该用户已被禁用');
        }
        //$where['user_id']=$user_id;
        $where['order_sn']=$order_sn;
        $where['pay_status']=array("in","2,3");
        $where['order_status']=array("neq","2");

        $list=M('order_info')->field('order_id,order_sn,wid,user_id,order_status,shipping_status,pay_status,mobile,consignee,order_amount,money_paid,surplus,shipping_extend_type,shipping_exman_name,shipping_exman_mobile')->where($where)->find();
        if($list['order_id']){
            if($list['order_status']==5 && $list['shipping_status']==2){
                $this->add_order_action($order_sn, $user_id, '配送完成,收货确认','',$list);
                $this->return_gk_success('此单已确认收货，不用重复确认');
            }
            try {
                if($_FILES['photo']['size']>100){
                    $path= "uploads/refund_pic/".rand(10000000,99999999).".".end(explode('.', $_FILES['photo']['name']));
                    Ali_ossApi::upload($path, $_FILES['photo']['tmp_name']);
                }

                $data['order_status']=5;
                $data['shipping_status']=2;
                $data['done_photo']=$path;
                $data['confirm_time']=gmtime();
                $data['wp_userid']=$user_id;
                $data['shipping_extend_status'] = $this->get_shipping_extend_status($status);    //第三方订单操作状态
                $data['outgo']=1;
                $id=M('order_info')->where($where)->save($data);
                if($id!=false){
                    A("Distribution")->order_buckle_inventory($list);
                    $a=array(
                        'order_id'=>$list['order_id'],
                        'user_id'=>$user_id,
                        'order_status'=>5,
                        'shipping_status'=>2,
                        'pay_status'=>$list['pay_status'],
                        'action_note'=>'配送完成,收货确认'
                    );

                    if(!empty($list['shipping_exman_name']) && !empty($list['shipping_exman_mobile'])){
                        $extend_data = array(
                            'shipping_extend_type'=>$list['shipping_extend_type'],
                            'shipping_exman_name'=>$list['shipping_exman_name'],
                            'shipping_exman_mobile'=>$list['shipping_exman_mobile']
                        );
                    }

                    A("Distribution")->record_diliveryman_log($a,$extend_data);//记录配送员操作日志
                    A("Distribution")->record_logistics($list['order_id'],2,"配送完成");
                    $wechat_param=array();

                    switch($_SERVER['SERVER_NAME']){
                        case 'api.xian3.com':
                            $thisurl='xian3.com';
                        break;
                        case 'api.test.shidexian.com':
                            $thisurl='test.shidexian.com';
                        break;
                        case 'api.shidexian.com':
                            $thisurl='w.shidexian.com';
                        break;
                        default:
                            $thisurl='w.shidexian.com';
                        break;
                    }
                    $oid=$list['user_id'];
                    $Code=md5($oid.'sdx@9999');

                    $goods_name=M("order_goods")->where(array('order_id'=>$list['order_id'],'goods_id'=>array('EXP','not in(select goods_id from sdx_refund_detail where order_id='.$list['order_id'].')')))->limit(1)->getField("goods_name");
                    $goods_number=M("order_goods")->where(array('order_id'=>$list['order_id'],'goods_id'=>array('EXP','not in(select goods_id from sdx_refund_detail where order_id='.$list['order_id'].')')))->count();

                    $louxiaoer_info=A("Smsapi")->get_louxiaoer_info($list['order_id']);

                    $housekeeper='';
                    if(!empty($louxiaoer_info['name']) && !empty($louxiaoer_info['phone'])){
                        $housekeeper="";
                    }else{
                        $housekeeper="";
                    }

                    $wechat_param['url']="https://".$thisurl."/?index&oid=".$oid."&code=".$Code."&lll=#!/ordInfo?ordId=".$list['order_id'];
                    $wechat_param['first']='尊敬的'.$list['consignee'].',您所订购的'.$goods_name.'等'.$goods_number.'件商品已经成功送达';
                    $wechat_param['keyword1']=$list['order_sn'];
                    $wechat_param['keyword2']=sprintf("%.2f",($list['order_amount']+$list['money_paid']+$list['surplus']));
                    $wechat_param['keyword3']=date("Y-m-d H:i:s");
                    $wechat_param['remark']=$housekeeper;

                    //发送订单配送完成短信给收货人
                    $send_result = A("Smsapi")->send_message_api('order_finish',$list['mobile'],$list['consignee'],0,$list['order_sn'],0,$list['user_id'],array(),'',$wechat_param);
                    A("Distribution")->insert_ready_sendmsg_order($list);

                    //确认收货，赠送红包
                    D('Userbonus')->send_bonus_to_user($list['user_id'],$list['order_id']);

                    $this->return_gk_success(array('id'=>$id),'收货确认');
                }else{
                    $this->return_gk_error('收货失败');
                }
            }catch (Exception $e){
                $this->return_gk_error($e->getMessage());
            }

        }else{
            $this->return_gk_error('查无此订单/订单状态不正常');
        }
    }

    /**
     * todo 操作订单回仓
     * @param $order_sn
     * @param $user_id
     * @param $action_note
     * @param $status
     */
    public function order_back($order_sn,$user_id,$action_note,$status){

        if(empty($order_sn)){
            $this->return_gk_error('订单号为空');
        }else{
            $order_info = M('order_info')->where(array('order_sn'=>$order_sn))->find();
            if(empty($order_info)){
                $this->return_gk_error('订单不存在');
            }

            if($order_info['outgo']==2){
                $this->return_gk_error('订单回仓进行中，不能操作回仓');
            }

            if($order_info['shipping_status']==2){
                $this->return_gk_error('订单已确认收货，不能操作回仓');
            }
            //配送员延迟两天以上，改预售单
            $data['shipping_status'] = 0;
            $data['wp_userid'] = 0;
//            $data['sorting_id'] = 0;
//            $data['sorting_time'] = 0;
            $data['outgo'] = 2;
            $data['shipping_extend_status'] = $this->get_shipping_extend_status($status);    //第三方订单操作状态
            $huic_test = "<span style='color: #ff0000;'>订单需要回仓，请将商品拿回总仓</span>";

            $map = array("order_id"=>$order_info['order_id']);

            $re = M('order_info')->where($map)->save($data);

            if($re!==false){
                if(empty($data['shipping_status'])){
                    $data['shipping_status'] = M('order_info')->where($map)->getField("shipping_status");
                }
                $a = array (
                    'order_id' => $order_info['order_id'],
                    'user_id' => $user_id,
                    'order_status' => OS_CONFIRMED,
                    'shipping_status' => $data['shipping_status'],
                    'pay_status' => PS_PAYED,
                    'action_note' => $action_note.",配送员已操作需回仓</br>".$huic_test
                );

                if(!empty($order_info['shipping_exman_name']) && !empty($order_info['shipping_exman_mobile'])){
                    $extend_data = array(
                        'shipping_extend_type'=>$order_info['shipping_extend_type'],
                        'shipping_exman_name'=>$order_info['shipping_exman_name'],
                        'shipping_exman_mobile'=>$order_info['shipping_exman_mobile']
                    );
                }

                A("Distribution")->record_diliveryman_log ($a,$extend_data); // 记录配送员操作日志

                M('warehouse_deliver_order_group')->where($map)->delete();
                $this->return_gk_success(null,'操作成功,商品需拿回总仓');
            }else{
                $this->return_gk_error('订单操作失败');
            }
        }
    }

    /**
     * @param $status
     * @return int
     */
    private function get_shipping_extend_status($status){
        switch ($status){
            case '10':
                $shipping_extend_status=1;   //配送中
            break;
            case '3':
                $shipping_extend_status=2;   //已撤销  待回仓
            break;
            case '90':
                $shipping_extend_status=3;   //配送员配送中发生异常事件，如：地址不正确、客户要求更改送达时间 待回仓
            break;
            case '5':
                $shipping_extend_status=6;   //已送达
            break;
            case '16':
                $shipping_extend_status=7;  //问题单   待回仓
            break;
            default:
                $shipping_extend_status=0;
            break;
        }

        return $shipping_extend_status;
    }


    public function add_order_action($order_sn,$user_id,$action,$extend_data=array(),$order=array(),$flag=true){
        if(empty($order)){
            $map = array('order_sn'=>$order_sn);
            $order = M('order_info')->where($map)->find();
        }

        $a=array(
            'order_id'=>$order['order_id'],
            'user_id'=>$user_id,
            'order_status'=>$order['order_status'],
            'shipping_status'=>$order['shipping_status'],
            'pay_status'=>$order['pay_status'],
            'action_note'=>$action
        );

        if(!empty($extend_data)){
            //保存第三方配送的配送信息（配送员，联系电话，配送类型）
            M('order_info')->where(array('order_id'=>$order['order_id']))->save($extend_data);
        }

        if(empty($extend_data) && !empty($order['shipping_extend_type']) && $flag){
            $extend_data = array(
                'shipping_extend_type'=>$order['shipping_extend_type'],
                'shipping_exman_name'=>$order['shipping_exman_name'],
                'shipping_exman_mobile'=>$order['shipping_exman_mobile']
            );
        }

        $res = A("Distribution")->record_diliveryman_log($a,$extend_data);//记录配送员操作日志
        return $res;
    }

    private function add_alarm_shipping_log($order_sn,$type,$msg){
        $param = array();
        $param['order_sn'] = $order_sn;
        $param['type'] = $type;
        $param['msg'] = $msg;
        $param['trigger_time'] = gmtime();
        $param['state'] = 0;
        $param['handle'] = '';
        $param['handle_time'] = 0;
        $param['source'] = $this->source;

        M('alarm_shipping_log')->add($param);
    }

    private function return_gk_error($msg){
        header("Content-type: text/html; charset=utf-8");
        $res['status']=0;
        $res['error_msg']=$msg;
        echo json_encode($res);
        exit;
    }

    private function return_gk_success($msg){
        header("Content-type: text/html; charset=utf-8");
        $res['status']=200;
        $res['error_msg']=$msg;
        echo json_encode($res);
        exit;
    }

}